package com.mvvm.start.main.mvvm.v.activity


import androidx.recyclerview.widget.LinearLayoutManager
import com.mvvm.start.base.base.BaseActivity
import com.mvvm.start.base.utils.SPUtils
import com.mvvm.start.common.mvvm.vm.PublicViewModle
import com.mvvm.start.main.R
import com.mvvm.start.main.bean.SwitchThemeBean
import com.mvvm.start.main.databinding.ActivitySwitchThemeBinding
import com.mvvm.start.main.mvvm.v.adapter.SwitchThemeAdapter
import skin.support.SkinCompatManager

/**
 * @功能: 主题切换界面
 * @User Lmy
 * @Creat 2021/9/29 12:01 下午
 * @Compony 永远相信美好的事情即将发生
 */
class SwitchThemeActivity : BaseActivity<PublicViewModle, ActivitySwitchThemeBinding>() {
    private var switchThemeAdapter = SwitchThemeAdapter()
    override fun initView() {
        viewBinding.toolbarplus.apply {
            toolBarBinding.tvTitle.text = "主题切换"
            toolBarBinding.toolbar.setNavigationOnClickListener {
                finish()
            }
        }
        viewBinding.apply {
            this.themeModle = mViewModel
        }
        switchThemeAdapter.apply {
            setOnItemClickListener { adapter, view, position ->
                for (value in data) {
                    value.isselect = false
                }
                data[position].isselect = true
                notifyDataSetChanged()
                SPUtils.getInstance(this@SwitchThemeActivity).put("themeType", data[position].type)
                SkinCompatManager.getInstance()
                    .loadSkin(
                        data[position].theme_name,
                        SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN
                    ) // 后缀加载
            }
        }
        switchThemeAdapter.setNewInstance(
            mutableListOf(
                SwitchThemeBean(R.drawable.swith_theme_colors_black, "主题黑", 1).apply {
                    isselect = getThemeType(1)
                },
                SwitchThemeBean(
                    R.drawable.swith_theme_colors_blue, "天空蓝" +
                            "", 2
                ).apply {
                    isselect = getThemeType(2)
                    theme_name = "blue"
                },
                SwitchThemeBean(R.drawable.swith_theme_colors_green, "早苗绿", 3).apply {
                    isselect = getThemeType(3)
                    theme_name = "green"
                },
                SwitchThemeBean(R.drawable.swith_theme_colors_red, "高能红", 4).apply {
                    isselect = getThemeType(4)
                    theme_name = "red"
                },
                SwitchThemeBean(R.drawable.swith_theme_colors_pink, "少女粉", 5).apply {
                    isselect = getThemeType(5)
                    theme_name = "pink"
                }

            )
        )
        viewBinding.recyclerview.apply {
            layoutManager = LinearLayoutManager(this@SwitchThemeActivity)
            adapter = switchThemeAdapter
        }
//        ktsetOnClickListener(cl_blue, cl_black) {
//            when (this) {
//                cl_black -> {
//                    SPUtils.getInstance(this@SwitchThemeActivity).put("themeType", 1)
//
//                }
//                cl_blue -> {
//
//                    SPUtils.getInstance(this@SwitchThemeActivity).put("themeType", 2)
//                }
//            }
//            initData()
//        }
    }


    /**
     * 获取当前主题类型
     */
    private fun getThemeType(type: Int) = SPUtils.getInstance(this@SwitchThemeActivity)
        .getSharedPreference("themeType", 1) as Int == type

    override fun initData() {
    }


    override fun initObserve() {
    }


}